package edu.sias.hyzn.controller;

import java.util.List;

import edu.sias.hyzn.vo.EyeAnalysisResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import edu.sias.common.annotation.Log;
import edu.sias.common.core.controller.BaseController;
import edu.sias.common.core.domain.AjaxResult;
import edu.sias.common.enums.BusinessType;
import edu.sias.hyzn.domain.Patient;
import edu.sias.hyzn.service.IPatientService;
import edu.sias.common.core.page.TableDataInfo;

/**
 * 患者信息Controller
 * 
 * @author Ailzr
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/hyzn/patient")
public class PatientController extends BaseController
{
    @Autowired
    private IPatientService patientService;

    /**
     * 查询患者信息列表
     */
//    @PreAuthorize("@ss.hasPermi('hyzn:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(Integer status, Patient patient)
    {
        if (status == null){
            status = 1;
        }
        startPage();
        List<EyeAnalysisResult> list = patientService.selectPatientList(patient, status);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermi('hyzn:patient:list')")
    @GetMapping("/info-list")
    public TableDataInfo infoList(Patient patient)
    {
        startPage();
        List<Patient> list = patientService.selectPatientInfoList(patient);
        return getDataTable(list);
    }

//    /**
//     * 导出患者信息列表
//     */
//    @PreAuthorize("@ss.hasPermi('hyzn:patient:export')")
//    @Log(title = "患者信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Patient patient)
//    {
//        List<EyeAnalysisResult> list = patientService.selectPatientList(patient);
//        ExcelUtil<Patient> util = new ExcelUtil<Patient>(Patient.class);
//        util.exportExcel(response, list, "患者信息数据");
//    }

    /**
     * 获取患者信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('hyzn:patient:query')")
    @GetMapping(value = "/{visit_id}")
    public AjaxResult getInfo(@PathVariable("visit_id") Long visit_id)
    {
        return success(patientService.selectVisitById(visit_id));
    }

    /**
     * 新增患者信息
     */
//    @PreAuthorize("@ss.hasPermi('hyzn:patient:add')")
    @Log(title = "患者信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Patient patient)
    {
        if (patient == null || patient.getIdCardNumber() == null){
            return AjaxResult.error(400, "患者信息不能为空");
        }
        Long patientId = patientService.insertPatient(patient);
        if (patientId == -1){
            return AjaxResult.error(500, "使用该身份证号的患者信息已经存在，请使用关联功能进行关联");
        }
        return success(patientId);
    }

    /**
     * 修改患者信息
     */
//    @PreAuthorize("@ss.hasPermi('hyzn:patient:edit')")
    @Log(title = "患者信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patient patient)
    {
        return toAjax(patientService.updatePatient(patient));
    }

    /**
     * 删除患者信息
     */
//    @PreAuthorize("@ss.hasPermi('hyzn:patient:remove')")
//    @Log(title = "患者信息", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(patientService.deletePatientByIds(ids));
//    }

//    @PreAuthorize("@ss.hasPermi('hyzn:patient:remove')")
    @Log(title = "患者信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(patientService.deleteVisitByIds(ids));
    }
}
